<template>
  <div>
      <div class="myRow">
        <div>
          <i-button class="myButton" type="info" ghost icon="md-add-circle"  @click="modal = true">新增</i-button>
          <i-button :disabled="selectList.length === 0" class="myButton" type="error" ghost icon="md-trash" @click="deleteList()">批量删除</i-button>
        </div>
        <div>
          <Input v-model="searchData" icon="ios-search" @on-click="getListFun" @on-enter="getListFun" placeholder="搜索内容" style="width: auto" />
        </div>
      </div>
      <i-table :loading="loading" border ref="selection" :columns="columns4" :data="listData.listT" @on-selection-change="selectChange"></i-table>
      <div class="footPage">
        <Page @on-change="changePage" :total="listData.recordCount" :page-size="listData.pageSize" :current="pageIndex"/>
      </div>
      <Modal
        v-model="modal"
        title="新增页面">
        <div>
          <i-form :model="addData" label-position="right" ref="addData" :rules="ruleValidate" :label-width="100">
            <FormItem label="模块：" prop="subordinateModule">
                <!-- <i-input v-model="addData.subordinateModule"></i-input> -->
                <Select v-model="addData.subordinateModule">
                    <Option v-for="item in modularlistData" :value="item.id" :key="item.id">{{ item.name }}</Option>
                </Select>
            </FormItem>
            <FormItem label="页面名称：" prop="pageName">
                <i-input v-model="addData.pageName"></i-input>
            </FormItem>
            <FormItem label="页面URL：" prop="pageIdentification">
                <i-input v-model="addData.pageIdentification"></i-input>
            </FormItem>
            <FormItem label="真实路径：" prop="pageRealPath">
                <i-input v-model="addData.pageRealPath"></i-input>
            </FormItem>
            <FormItem label="页面描述：" prop="pageDescription">
                <i-input v-model="addData.pageDescription" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="页面描述"></i-input>
            </FormItem>
            <FormItem label="布局页面：" prop="pageTemplate">
              <RadioGroup v-model="addData.pageTemplate">
                  <Radio label="Layout"></Radio>
                  <Radio label="TestLayout"></Radio>
              </RadioGroup>
            </FormItem>
          </i-form>
        </div>
        <div slot="footer">
            <i-button type="text" :loading="modal_loading" @click="reset('addData')">重置</i-button>
            <i-button type="primary" :loading="modal_loading" @click="ok('addData')">确定</i-button>
        </div>
    </Modal>
    <Modal
        v-model="modal2"
        title="修改页面">
        <div>
          <i-form :model="editData" label-position="right" ref="editData" :rules="ruleValidate" :label-width="100">
            <FormItem label="模块：" prop="subordinateModule">
                <!-- <i-input v-model="editData.subordinateModule"></i-input> -->
                <Select v-model="editData.subordinateModule">
                    <Option v-for="item in modularlistData" :value="item.id" :key="item.id">{{ item.name }}</Option>
                </Select>
            </FormItem>
            <FormItem label="页面名称：" prop="pageName">
                <i-input v-model="editData.pageName"></i-input>
            </FormItem>
            <FormItem label="页面URL：" prop="pageIdentification">
                <i-input v-model="editData.pageIdentification"></i-input>
            </FormItem>
            <FormItem label="真实路径：" prop="pageRealPath">
                <i-input v-model="editData.pageRealPath"></i-input>
            </FormItem>
            <FormItem label="页面描述：" prop="pageDescription">
                <i-input v-model="editData.pageDescription" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="页面描述"></i-input>
            </FormItem>
            <FormItem label="布局页面：" prop="pageTemplate">
              <RadioGroup v-model="editData.pageTemplate">
                  <Radio label="Layout"></Radio>
                  <Radio label="TestLayout"></Radio>
              </RadioGroup>
            </FormItem>
            <FormItem label="页面排序：" prop="pageOrder">
              <InputNumber :min="0" v-model="editData.pageOrder" :editable="false"></InputNumber>
            </FormItem>
          </i-form>
        </div>
        <div slot="footer">
            <i-button type="text" :loading="modal_loading" @click="reset2('editData')">重置</i-button>
            <i-button type="primary" :loading="modal_loading" @click="editFun('editData')">保存</i-button>
        </div>
    </Modal>
  </div>
</template>

<script>
import {
  GetPermissionPageConfigPager,
  AddPermissionPageConfig,
  UpdatePermissionPageConfig,
  UpdatePermissionPageConfigByIdsOrId
} from "@/api/permissionPageConfig";
import { GetPermissionModularAllInfo } from "@/api/permissionModular";
export default {
  name: "home",
  components: {},
  data() {
    return {
      ruleValidate: {
        subordinateModule: [
          {
            required: true,
            message: "请选择模块",
            trigger: "change"
          }
        ],
        pageName: [
          {
            required: true,
            message: "请输入页面名称",
            trigger: "blur"
          }
        ],
        pageRealPath: [
          {
            required: true,
            message: "请输入真实路径",
            trigger: "blur"
          }
        ],
        pageIdentification: [
          {
            required: true,
            message: "请输入页面URL",
            trigger: "blur"
          }
        ],
        pageDescription: [
          {
            required: true,
            message: "请输入页面描述",
            trigger: "blur"
          }
        ]
      },
      modularlistData: [], // 模块下拉数据
      searchData: "",
      selectList: [], // 选中列表
      selectStr: "", // 选中列表id字符串
      pageIndex: 1, // 当前页
      modal_loading: false,
      addData: {
        subordinateModule: "",
        pageName: "",
        pageRealPath: "",
        pageTemplate: "Layout",
        pageIdentification: "",
        pageDescription: ""
      },
      editData: {
        subordinateModule: "",
        pageName: "",
        pageRealPath: "",
        pageIdentification: "",
        pageDescription: "",
        pageTemplate: "Layout",
        pageOrder: 0
      },
      editId: "",
      editDelete: "",
      modal: false,
      modal2: false,
      loading: false,
      columns4: [
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: "ID",
          type: "index",
          width: 80,
          align: "center"
        },
        {
          title: "模块",
          key: "subordinateModuleName",
          align: "center"
        },
        {
          title: "页面名称",
          key: "pageName",
          align: "center"
        },
        {
          title: "真实路径",
          key: "pageRealPath",
          align: "center"
        },
        {
          title: "URL",
          key: "pageIdentification",
          align: "center"
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.deleteBtn(params);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      listData: {}
    };
  },
  mounted() {
    this.getListFun();
    this.getModularListFun();
  },
  methods: {
    instance(confirmationBoxContent) {
      const title = confirmationBoxContent.title;
      const content = confirmationBoxContent.contentText;
      this.$Modal.confirm({
        title: title,
        content: content,
        onOk: () => {
          this.deleteFun(confirmationBoxContent.params);
        },
        onCancel: () => {
          this.$Message.info("操作取消");
        }
      });
    },
    // 获取模块下拉数据
    getModularListFun() {
      debugger;
      GetPermissionModularAllInfo()
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "modularlistData", res.data.result);
            debugger;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 获取用户列表数据
    getListFun() {
      this.$set(this, "loading", true);
      const p = {
        pageIndex: this.pageIndex,
        pageSize: "10",
        keyWord: this.searchData
      };
      GetPermissionPageConfigPager(p)
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "listData", res.data.result);
          }
          this.$set(this, "loading", false);
        })
        .catch(err => {
          console.log(err);
          this.$set(this, "loading", false);
        });
    },
    // 新增接口
    addFun() {
      const p = {
        subordinateModule: this.addData.subordinateModule,
        pageName: this.addData.pageName,
        pageIdentification: this.addData.pageIdentification,
        pageRealPath: this.addData.pageRealPath,
        pageTemplate: this.addData.pageTemplate,
        pageDescription: this.addData.pageDescription
      };
      AddPermissionPageConfig(p)
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "addData", {
              subordinateModule: "",
              pageName: "",
              pageIdentification: "",
              pageRealPath: "",
              pageTemplate: "Layout",
              pageDescription: ""
            });
            this.getListFun();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    custom() {
      this.$Modal.confirm({
        title: "Title",
        content: "<p>Content of dialog</p><p>Content of dialog</p>",
        okText: "OK",
        cancelText: "Cancel"
      });
    },
    // 分页回调方法
    changePage(index) {
      this.$set(this, "pageIndex", index);
      this.getListFun();
    },
    ok(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.addFun();
          this.$set(this, "modal", false);
        } else {
          this.$Message.error("提交内容有误！请检查");
        }
      });
    },
    editFun(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          const p = {
            subordinateModule: this.editData.subordinateModule,
            pageName: this.editData.pageName,
            pageOrder: this.editData.pageOrder,
            pageIdentification: this.editData.pageIdentification,
            pageRealPath: this.editData.pageRealPath,
            pageDescription: this.editData.pageDescription,
            pageTemplate: this.editData.pageTemplate,
            id: this.editId,
            editDelete: this.editDelete
          };
          UpdatePermissionPageConfig(p)
            .then(res => {
              if (res.data.code === "0") {
                this.$set(this, "editData", {
                  subordinateModule: "",
                  pageName: "",
                  pageIdentification: "",
                  pageRealPath: "",
                  pageDescription: "",
                  pageTemplate: "Layout",
                  pageOrder: 0
                });
                this.getListFun();
              }
            })
            .catch(err => {
              console.log(err);
            });
          this.$set(this, "modal2", false);
        } else {
          this.$Message.error("提交内容有误！请检查");
        }
      });
    },
    edit(params) {
      debugger;
      this.$set(this, "editData", {
        subordinateModule: params.row.subordinateModule.toString(),
        pageName: params.row.pageName,
        pageOrder: params.row.pageOrder,
        pageIdentification: params.row.pageIdentification,
        pageRealPath: params.row.pageRealPath,
        pageDescription: params.row.pageDescription,
        pageTemplate: params.row.pageTemplate
      });
      this.$set(this, "editId", params.row.id);
      this.$set(this, "editDelete", params.row.editDelete);
      this.$set(this, "modal2", true);
    },
    deleteFun(ids) {
      const p = {
        idsStr: ids,
        isDelete: 1
      };
      UpdatePermissionPageConfigByIdsOrId(p)
        .then(res => {
          if (res.data.code === "0") {
            this.$set(this, "selectList", []);
            this.$set(this, "selectStr", "");
            this.getListFun();
          }
        })
        .catch(err => {
          console.log(err);
        });
      this.$set(this, "modal2", false);
    },
    deleteBtn(params) {
      var confirmationBoxContent = {
        title: "确认删除",
        contentText: "一旦删除不可撤销，确认删除？",
        params: String(params.row.id)
      };
      this.instance(confirmationBoxContent);
    },
    reset(name) {
      this.$refs[name].resetFields();
      this.$set(this, "addData", {
        subordinateModule: "",
        pageName: "",
        pageIdentification: "",
        pageRealPath: "",
        pageTemplate: "Layout",
        pageDescription: ""
      });
    },
    deleteList() {
      var confirmationBoxContent = {
        title: "确认删除",
        contentText: "一旦删除不可撤销，确认删除？",
        params: String(this.selectStr)
      };
      this.instance(confirmationBoxContent);
    },
    selectChange(list) {
      var str = "";
      this.$set(this, "selectStr", "");
      for (var i = 0; i < list.length; i++) {
        str = str + "," + list[i].id;
      }
      this.$set(this, "selectList", list);
      this.$set(this, "selectStr", str);
    },
    reset2(name) {
      this.$refs[name].resetFields();
      this.$set(this, "editData", {
        subordinateModule: "",
        pageName: "",
        pageIdentification: "",
        pageRealPath: "",
        pageDescription: "",
        pageTemplate: "Layout",
        pageOrder: 0
      });
    }
  }
};
</script>

<style lang="less">
.myRow {
  width: 100%;
  margin: 0px 0px 10px;
  display: flex;
  justify-content: space-between;
}
.myButton {
  margin-right: 6px;
}
.footPage {
  width: 100%;
  margin-top: 20px;
  text-align: center;
}
</style>
